<?php
/**
 * @link http://www.tintsoft.com/
 * @copyright Copyright (c) 2013 TintSoft LLC
 * @license http://www.tintsoft.com/license/
 */
namespace Leaps\Exception;
use Leaps\Base\ServiceProvider;
class ExceptionServiceProvider extends ServiceProvider
{
    /**
     * 注册服务提供者。
     *
     * @return void
     */
    public function register()
    {
        $this->registerDisplayers ();
        $this->registerHandler ();
    }

    /**
     * 注册异常显示
     *
     * @return void
     */
    protected function registerDisplayers()
    {
        $this->registerPlainDisplayer ();
        $this->registerDebugDisplayer ();
    }

    /**
     * 注册异常处理程序实例
     *
     * @return void
     */
    protected function registerHandler()
    {
        $this->app ['exception'] = $this->app->share ( function ($app)
        {
            return new Handler ( $app ['exception.plain'], $app ['exception.debug'] );
        } );
    }

    /**
     * 注册简单的异常显示器。
     *
     * @return void
     */
    protected function registerPlainDisplayer()
    {
        $this->app ['exception.plain'] = $this->app->share ( function ($app)
        {
            $me = $this;
            $handler = new PlainDisplayer ($app['config']['app']['charset'],$app->isConsole (),$app->getFrameworkVersion());
            if ( ! is_null ( $path = $me->resourcePath () ) ) {
                $handler->setResourcesPath ( $path );
            }
            return $handler;
        } );
    }

    protected function registerDebugDisplayer()
    {
        $this->app ['exception.debug'] = $this->app->share ( function ($app)
        {
            return new WhoopsDisplayer ( $app ['whoops'], $app->isConsole () );
        } );
    }


    /**
     * 注册服务提供者。
     *
     * @return void
     */
    public function register1()
    {
        $this->app ['exception'] = $this->app->share ( function ($app)
        {
            return new ErrorHandler;
        } );
    }
}